#include "../inc/Ui/BookmarkPlaceViewerForm.h"
#include "../inc/Ui/WebForm.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Base::Collection;
using namespace Osp::Graphics;
using namespace Osp::Media;

int BookmarkPlaceViewerForm::FormId = -1;

BookmarkPlaceViewerForm::BookmarkPlaceViewerForm(void) {
}

BookmarkPlaceViewerForm::~BookmarkPlaceViewerForm(void) {
}

bool BookmarkPlaceViewerForm::Initialize() {
	Form::Construct(L"BookmarkPlaceViewerForm");

	return true;
}

result BookmarkPlaceViewerForm::OnInitializing(void) {
	result r = E_SUCCESS;
	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_SHOWMAP);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);

	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	this->AddSoftkeyActionListener(SOFTKEY_0, *this);

	pExpandablelist = static_cast<ExpandableList *> (GetControl(
			"IDC_EXPANDABLELIST1"));
	if (pExpandablelist) {
		pExpandablelist->AddExpandableItemEventListener(*this);
		pExpandablelist->AddTouchEventListener(*this);
	}
	pEditAreaChuThich = static_cast<EditArea*> (GetControl(
			"IDC_EDITAREA_GHICHU"));
	isDelete = false;
	return r;
}

result BookmarkPlaceViewerForm::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}
void BookmarkPlaceViewerForm::OnActionPerformed(const Control& source,
		int actionId) {

	FormManager *pFormManager = null;
	Form *pWebForm = null;
	FormManager::GetInstance(pFormManager);
	switch (actionId) {
	case ID_SOFTKEY_SHOWMAP: {
		/*	pFrame->SetCurrentForm(*pFormParent);
		 pFormParent->RequestRedraw(true);
		 */
		pFormManager->SetCurrentForm(WebForm::GetFormId());
		result r = pFormManager->GetForm(WebForm::GetFormId(),pWebForm);
		if (IsFailed(r)) {
			return;
		}
		static_cast<WebForm*> (pWebForm)->DatCacMarkerViTriYeuThichLenMap();
	}
		break;
	case ID_SOFTKEY_BACK: {
		//pFrame->SetCurrentForm(*pFormParent);
		//pFormParent->RequestRedraw(true);
		pFormManager->SetCurrentForm(WebForm::GetFormId());
	}
		break;
	default:
		break;

	}
}/*
 void BookmarkPlaceViewerForm::SetParentForm(Form *pParent) {
 this->pFormParent = pParent;
 }

 void BookmarkPlaceViewerForm::SetFrame(Frame *pFrame) {
 this->pFrame = pFrame;
 }*/

Bitmap*
BookmarkPlaceViewerForm::GetBitmapN(const Osp::Base::String& name) {
	Bitmap* pBitmap = null;
	Image* pImage = new Image();

	String fullname(L"/Res/imgPlace/");
	fullname.Append(name);
	AppLog("Get bitmap:%S", fullname.GetPointer());
	pImage->Construct();

	if(fullname.EndsWith(L"jpg"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	else if(fullname.EndsWith(L"bmp"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	else if(fullname.EndsWith(L"png"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_ARGB8888);
	}
	else if (fullname.EndsWith(L"gif"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	delete pImage;

	return pBitmap;

}

result BookmarkPlaceViewerForm::AddListMainItem(ExpandableList& expandableList,
		String itemText, Bitmap* pBitmapNormal, Bitmap* pBitmapFocused) {
	result r = E_SUCCESS;
	// Create a main item of the ExpandableList
	CustomListItem* pMainItem = new CustomListItem();
	pMainItem->Construct(100);
	pMainItem->SetItemFormat(*__pMainItemFormat);
	pMainItem->SetElement(ID_LIST_BITMAP, *pBitmapNormal, pBitmapFocused);
	pMainItem->SetElement(ID_LIST_TEXT, itemText);

	// Add a item to the ExpandableList
	expandableList.AddItem(*pMainItem, ID_LIST_MAINITEM);
	return r;
}

result BookmarkPlaceViewerForm::AddListSubItem(int indexMainItem,
		ExpandableList& expandableList, int itemId, String itemText) {
	result r = E_SUCCESS;
	// Create a sub-item of the ExpandableList
	CustomListItem* pSubItem = new CustomListItem();
	pSubItem->Construct(100);
	pSubItem->SetItemFormat(*__pSubItemFormat);
	pSubItem->SetElement(ID_LIST_TEXT, itemText);
	// Add a item to the ExpandableList
	expandableList.AddSubItem(indexMainItem, *pSubItem, ID_LIST_SUBITEM);
	return r;
}
void BookmarkPlaceViewerForm::loadDuLieuChoExpandable(
		ExpandableList *pExpandableList) {
	// Create Bitmap
	Image *pImage = new Image();
	pImage->Construct();
	Bitmap *pBitmapFocused = pImage->DecodeN(L"/Res/imgPlace/commentplace.png", BITMAP_PIXEL_FORMAT_RGB565);

	/* Create an item format of the main item*/
	__pMainItemFormat = new CustomListItemFormat();
	__pMainItemFormat->Construct();

	__pMainItemFormat->AddElement(ID_LIST_TEXT, Osp::Graphics::Rectangle(110, 30, 250, 50),38);
	__pMainItemFormat->AddElement(ID_LIST_BITMAP, Osp::Graphics::Rectangle(10, 15, 70, 70));

	/* Create an item format of the sub-item*/
	__pSubItemFormat = new CustomListItemFormat();
	__pSubItemFormat->Construct();
	__pSubItemFormat->AddElement(ID_LIST_TEXT, Osp::Graphics::Rectangle(20, 25,450, 50),32);

	/*Bat dau dua du lieu thoi thiet vao expandlist*/
	MyParserXml *myPa = new MyParserXml();
	list = new LinkedListT<MyViTriYeuThich*> ();
	list = myPa->getLayMangViTriYeuThich("/Home/ViTriYeuThich.xml");
	int id = 0;
	for (int i = 0; i < list->GetCount(); i++) {
		MyViTriYeuThich *p = new MyViTriYeuThich();
		list->GetAt(i, p);
		/* Add main item*/
		Bitmap *pBitmapNormal =GetBitmapN("commentplace.png");
		AddListMainItem(*pExpandableList, p->__pTieuDe, pBitmapNormal,pBitmapFocused);
		/* Add sub-item*/
		AddListSubItem(i, *pExpandableList, id++, p->__pChuThich);
		delete pBitmapNormal;
	}
	//Ket thuc viec dua du lieu thoi tiet vao expandlist
	/* Display the Form*/
	Draw();
	Show();
	/* Deallocate a Bitmap*/
	delete pBitmapFocused;
}
void BookmarkPlaceViewerForm::loadViTriYeuThichLenExpandable() {
	pExpandablelist->RemoveAllItems();
	loadDuLieuChoExpandable(pExpandablelist);
}

void BookmarkPlaceViewerForm::OnItemStateChanged(
		const Osp::Ui::Control &source, int mainIndex, int subIndex,
		int itemId, int elementId, Osp::Ui::ItemStatus status) {
	switch (itemId) {
	case ID_LIST_MAINITEM:
		AppLog("Main");
		// Todo:
		break;
	case ID_LIST_SUBITEM:
		AppLog("Sub");
		// Todo:
		break;
	default:
		break;
	}

}

void BookmarkPlaceViewerForm::OnItemStateChanged(
		const Osp::Ui::Control &source, int mainIndex, int subIndex,
		int itemId, Osp::Ui::ItemStatus status) {
	switch (itemId) {
	case ID_LIST_MAINITEM: {
		AppLog("Main:%d", mainIndex);
		if (isDelete == true) {
			AppLog("Main:Ban da yeu cau lenh xoa");
			xoaViTriYeuThich(mainIndex);
		}
	}
		break;
	case ID_LIST_SUBITEM: {
		AppLog("Main: %d ,Sub:%d",mainIndex, subIndex);
		hienThiChiTietNoiDungGhiChu(mainIndex);
		if (isDelete == true) {
			AppLog("Sub : Ban yeu cau lenh xoa");
		}
	}
		break;
	default:
		break;
	}
	isDelete = false;
}
void BookmarkPlaceViewerForm::hienThiChiTietNoiDungGhiChu(int mainIndex) {

	String ghiChu;
	MyViTriYeuThich *pMyViTri = new MyViTriYeuThich();
	list->GetAt(mainIndex, pMyViTri);
	ghiChu = pMyViTri->__pChuThich;
	pEditAreaChuThich->SetText(ghiChu);
	pEditAreaChuThich->RequestRedraw(true);
	pEditAreaChuThich->Show();
	pEditAreaChuThich->Draw();
}
void BookmarkPlaceViewerForm::xoaViTriYeuThich(int index) {
	MyViTriYeuThich *pMyViTriYeuThich = new MyViTriYeuThich();
	list->GetAt(index, pMyViTriYeuThich);
	MyParserXml *pMyParserXml = new MyParserXml();
	pMyParserXml->xoaNodeInXml("/Home/ViTriYeuThich.xml",
			pMyViTriYeuThich->__pLat, pMyViTriYeuThich->__pLng);
	loadViTriYeuThichLenExpandable();
}
void BookmarkPlaceViewerForm::OnTouchDoublePressed(
		const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	AppLog("Touch double press");
}

void BookmarkPlaceViewerForm::OnTouchFocusIn(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	AppLog("Touch double focus in");
}

void BookmarkPlaceViewerForm::OnTouchFocusOut(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	AppLog("Touch focus out");
}

void BookmarkPlaceViewerForm::OnTouchLongPressed(
		const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	AppLog("Touch long press");
	isDelete = true;
}

void BookmarkPlaceViewerForm::OnTouchMoved(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	AppLog("Touch move");
}

void BookmarkPlaceViewerForm::OnTouchPressed(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	AppLog("Touch press");
}

void BookmarkPlaceViewerForm::OnTouchReleased(const Osp::Ui::Control &source,
		const Osp::Graphics::Point &currentPosition,
		const Osp::Ui::TouchEventInfo &touchInfo) {
	AppLog("Touch release");
}
